package cn.xn.hrms.api.controller;

import cn.xn.hrms.api.dto.res.PersonalTrainDTO;
import cn.xn.hrms.domain.service.PersonalTrainService;
import cn.xn.hrms.domain.utils.ResultVo;
import cn.xn.hrms.api.dto.req.PersonalTrainReqDTO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author ysz
 */
@RestController
public class PersonalTrainController {

    @Resource
    PersonalTrainService personalTrainService;

    /**
     * 分页查询员工培训列表
     *
     * @param page
     * @param size
     * @param departmentId
     * @param personalId
     * @param beginDate
     * @param endDate
     * @return
     */
    @GetMapping("/train/list")
    @ApiOperation("分页查询员工培训列表")
    public PageInfo<PersonalTrainDTO> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size,
                                           @RequestParam(value = "departmentId", required = false) Integer departmentId,
                                           @RequestParam(value = "personalId", defaultValue = "0") Integer personalId,
                                           @RequestParam(value = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                           @RequestParam(value = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {
        return personalTrainService.findAll(page, size, departmentId, personalId, beginDate, endDate);
    }

    /**
     * 员工培训列表
     *
     * @return
     */
    @GetMapping("/train/all")
    @ApiOperation("员工培训列表")
    public List<PersonalTrainDTO> all() {
        return personalTrainService.all();
    }

    /**
     * 新增员工培训
     *
     * @param personalTrainReqDTO
     * @return
     */
    @PostMapping("/train")
    @ApiOperation("新增员工培训")
    public ResultVo insert(@RequestBody @Valid PersonalTrainReqDTO personalTrainReqDTO) {
        personalTrainService.insert(personalTrainReqDTO);
        return ResultVo.success();
    }

    /**
     * 修改员工培训
     *
     * @param personalTrainReqDTO
     * @param id
     * @return
     */
    @PutMapping("/train/{id}")
    @ApiOperation("修改员工培训")
    public ResultVo update(@RequestBody @Valid PersonalTrainReqDTO personalTrainReqDTO, @PathVariable Integer id) {
        personalTrainService.updateById(id, personalTrainReqDTO);
        return ResultVo.success();
    }

    /**
     * 删除员工培训(支持批量)
     *
     * @param id
     * @return
     */
    @DeleteMapping("/train/delete")
    @ApiOperation("删除员工培训(支持批量)")
    public ResultVo deleteIdIn(@RequestBody Set<Integer> id) {
        personalTrainService.deleteByIdIn(id);
        return ResultVo.success();
    }
}
